<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\common\model;

/**
 * 接口模型
 */
class Api extends ModelBase
{
    
    /**
     * 请求数据获取器
     */
    public function getRequestDataAttr($value = null, $data = [])
    {
        
        $raw = array_key_exists('request_data', $data)
            ? $data['request_data']
            : (is_array($this->getData()) && array_key_exists('request_data', $this->getData()) ? $this->getData()['request_data'] : '');
        return json_decode($raw, true);
    }
    
    /**
     * 响应数据获取器
     */
    public function getResponseDataAttr($value = null, $data = [])
    {
        
        $raw = array_key_exists('response_data', $data)
            ? $data['response_data']
            : (is_array($this->getData()) && array_key_exists('response_data', $this->getData()) ? $this->getData()['response_data'] : '');
        return json_decode($raw, true);
    }
    
    /**
     * API分组获取器
     */
    public function getGroupNameAttr($value = null, $data = [])
    {
        
        $groupId = array_key_exists('group_id', $data)
            ? $data['group_id']
            : (is_array($this->getData()) && array_key_exists('group_id', $this->getData()) ? $this->getData()['group_id'] : null);
        
        return is_null($groupId) ? '' : $this->modelApiGroup->getValue(['id' => $groupId], 'name');
    }
    
    /**
     * 请求类型获取器
     */
    public function getRequestTypeTextAttr($value = null, $data = [])
    {
        
        $requestType = array_key_exists('request_type', $data)
            ? $data['request_type']
            : (is_array($this->getData()) && array_key_exists('request_type', $this->getData()) ? $this->getData()['request_type'] : 0);
        return $requestType ? 'GET' : 'POST';
    }
    
    /**
     * API状态获取器
     */
    public function getApiStatusTextAttr($value = null, $data = [])
    {
        
        $array = parse_config_array('api_status_option');
        $apiStatus = array_key_exists('api_status', $data)
            ? $data['api_status']
            : (is_array($this->getData()) && array_key_exists('api_status', $this->getData()) ? $this->getData()['api_status'] : null);
        
        return is_null($apiStatus) ? '' : ($array[$apiStatus] ?? '');
    }
    
    /**
     * API研发者获取器
     */
    public function getDeveloperTextAttr($value = null, $data = [])
    {
        
        $array = parse_config_array('team_developer');
        $developer = array_key_exists('developer', $data)
            ? $data['developer']
            : (is_array($this->getData()) && array_key_exists('developer', $this->getData()) ? $this->getData()['developer'] : null);
        
        return is_null($developer) ? '' : ($array[$developer] ?? '');
    }
}
